from django.conf import settings
from django.db.models import Avg, Sum, Count, Max, Min, Q, F

from rest_framework import serializers
from rest_framework.serializers import *
from rest_framework.validators import UniqueValidator, UniqueTogetherValidator  # 验证器

from mysqldb.models import *
from mysqldb import redis as REDIS


class ActivePaySerializerV2(serializers.Serializer):
    """用户主扫收款"""

    pid = IntegerField(help_text="场所ID")
    tid = IntegerField(help_text="经营名目ID")
    reqsn = CharField(help_text="商户唯一交易凭证号码")
    trxamt = IntegerField(min_value=1, help_text="交易金额,单位为分")
    paytype = ChoiceField(["W01", "A01"], required=True, help_text="交易方式")

    class Meta:
        fields = "__all__"

    def validate_pid(self, attr):
        try:
            place = AllinpayPlaceList.objects.get(id=attr, status__gt=0)
        except BaseException as err:
            raise serializers.ValidationError("未找到有效的经营场所")
        return attr

    def validate(self, attrs):
        try:
            bind = AllinpayTermList.objects.get(
                id=attrs["tid"], pid=attrs["pid"], status__gt=0
            )
        except BaseException as err:
            raise serializers.ValidationError({"tid": "未找到经营名目"})
        return attrs


class SweptPaySerializerV2(serializers.Serializer):
    """用户被扫收款"""

    pid = IntegerField(help_text="场所ID")
    tid = IntegerField(help_text="经营名目ID")
    reqsn = CharField(max_length=32, help_text="商户唯一交易凭证号码")
    trxamt = IntegerField(min_value=1, help_text="交易金额,单位为分")
    authcode = CharField(max_length=32, help_text="付款码")

    class Meta:
        fields = "__all__"

    def validate_pid(self, attr):
        try:
            place = AllinpayPlaceList.objects.get(id=attr, status__gt=0)
        except BaseException as err:
            raise serializers.ValidationError("未找到有效的经营场所")
        return attr

    def validate(self, attrs):
        try:
            bind = AllinpayTermList.objects.get(
                id=attrs["tid"], pid=attrs["pid"], status__gt=0
            )
        except BaseException as err:
            raise serializers.ValidationError({"tid": "未找到经营名目"})
        return attrs


class AlertSerializerV2(serializers.Serializer):
    """消息通知接口"""

    appid = CharField(allow_blank=True, allow_null=True, help_text="收银宝APPID")
    outtrxid = CharField(allow_blank=True, allow_null=True, help_text="第三方交易号")
    trxcode = CharField(allow_blank=True, allow_null=True, help_text="交易类型")
    trxid = CharField(allow_blank=True, allow_null=True, help_text="收银宝交易单号")
    trxdate = CharField(allow_blank=True, allow_null=True, help_text="交易请求日期")
    paytime = CharField(allow_blank=True, allow_null=True, help_text="交易完成时间")
    trxstatus = CharField(allow_blank=True, allow_null=True, help_text="交易结果码")
    cusid = CharField(allow_blank=True, allow_null=True, help_text="商户编号")
    cusorderid = CharField(allow_blank=True, allow_null=True, help_text="业务流水")
    sign = CharField(allow_blank=True, allow_null=True, help_text="sign校验码")
    trxamt = IntegerField(default=0, help_text="交易金额")
    initamt = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="原始下单金额"
    )
    chnltrxid = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="渠道流水号"
    )
    termno = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="终端编号"
    )
    termbatchid = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="终端批次号"
    )
    termtraceno = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="终端流水号"
    )
    termauthno = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="终端授权码"
    )
    termrefnum = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="终端参考号"
    )
    trxreserved = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="业务关联内容"
    )
    srctrxid = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="原交易流水"
    )
    acct = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="交易账号"
    )
    fee = CharField(allow_blank=True, allow_null=True, required=False, help_text="手续费")
    signtype = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="签名类型"
    )
    cmid = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="渠道子商户号"
    )
    chnlid = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="渠道号"
    )
    chnldata = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="渠道信息"
    )
    accttype = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="借贷标识"
    )

    class Meta:
        fields = "__all__"


class PlaceStoredReqsnSerializer(serializers.ModelSerializer):
    """场所储值订单序列化"""

    id = IntegerField(label="ID", read_only=True)
    trxamt = IntegerField(allow_null=True, min_value=0, help_text="交易金额,单位为分")
    pid = IntegerField(allow_null=True, help_text="场所ID")
    status = IntegerField(allow_null=True, max_value=1, min_value=0, default=0)
    reqsn = CharField(read_only=True)
    currentime = DateTimeField(read_only=True)

    class Meta:
        model = AllinpayPlacestoredReqsn
        fields = "__all__"

    def validate_trxamt(self, attr):
        if attr > 0:
            return attr
        else:
            raise serializers.ValidationError("金额错误")

    def validate_pid(self, attr):
        try:
            place = AllinpayPlaceList.objects.get(id=attr, status__gt=0)
        except BaseException as err:
            raise serializers.ValidationError("未找到有效的经营场所")
        return attr


class PlaceStoredPaySerializer(serializers.Serializer):
    """场所储值支付序列化"""

    order = CharField(help_text="原始单号")
    paytype = ChoiceField(["W01", "A01"], required=True, help_text="交易方式")
    # reqsn = CharField(help_text="商户唯一交易凭证号码")

    class Meta:
        fields = "__all__"


# print(repr(PlaceStoredReqsnSerializer()))
